package com.tcs.test.executor.queue;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tcs.test.executor.base.BeseExecutorRun;

public class QueueTest1 extends BeseExecutorRun {

	private final Logger logger = LoggerFactory.getLogger(QueueTest1.class);

	private LinkedBlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>(5);

	@Override
	protected void service() {
//		new Thread(new OfferRun(), "offer").start();
//		new Thread(new TakeRun() , "take").start();
		Map<String,String> map = new HashMap<String,String>();
		for (int i = 0; i < 10; i++) {
			map.put("key" + i, null);
		}
	}

	class OfferRun implements Runnable {

		@Override
		public void run() {
			int e = 0;
			while (true) {
				if (!queue.offer(++e)) {
					try {
						queue.put(e);
					} catch (InterruptedException e1) {
						logger.error("", e1);
					}
				}
				logger.info(" offer : {} ", e);
			}
		}

	}

	class TakeRun implements Runnable {

		@Override
		public void run() {
			Integer e = 0;
			while (true) {
				e = queue.poll();
				if (e == null) {
					try {
						e = queue.take();
					} catch (InterruptedException e1) {
						logger.error("", e1);
					}
				}
				logger.info(" take : {} ", e);
			}
		}

	}

	@Test
	public void go() {
		try {
			super.go();
		} catch (InterruptedException e) {
			logger.error("", e);
		}
	}
}